package com.jd.tuning.jvm.objectpool.datasource;

import org.apache.commons.pool2.impl.GenericObjectPool;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;

import java.util.HashMap;
import java.util.Map;


/**
 * 监控
 */
//可用用/actuator/datasource
@Endpoint(id = "datasource")  //端点
public class DataSourceEndpoint {

    private CoolDataSource coolDataSource;

    public DataSourceEndpoint(CoolDataSource coolDataSource) {
        this.coolDataSource=coolDataSource;
    }

    @ReadOperation        //get 方法访问调用
    public Map<String,Object> pool(){
        GenericObjectPool<MyConnection> pool= coolDataSource.getPool();
        Map<String,Object> map=new HashMap<>();
        map.put("numActive",pool.getNumActive());
        map.put("numIdle",pool.getNumIdle());
        map.put("createdCount",pool.getCreatedCount());
        return map;
    }



}
